//
//  HMHomeTableViewController.swift
//  sina
//
//  Created by 崔正强 on 17/3/19.
//  Copyright © 2017年 崔正强. All rights reserved.
//

import UIKit
import YYModel
//cell可重用标识符
private let CELLID = "reuseIdentifier"

class HMHomeTableViewController: HMBaseTableViewController {

    //保存模型数组
//    var dataArray:[HMHomeModel] = [HMHomeModel]()
    //viewModel
    var homeViewModel: HMHomeViewModel = HMHomeViewModel()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setNavBar()
        
        if !userLogin {
            visitorView.updataUI(title: "关注一些人,回到这里看看有什么惊喜", imageName: "visitordiscover_feed_image_smallicon",isHome: true)
            return
        }
        setupTableViewInfo()
        loadData()
    }

}

//设置tableView的相关信息
extension HMHomeTableViewController{
    fileprivate func setupTableViewInfo(){
        //注册cell
        tableView.register(HMHomeCell.self, forCellReuseIdentifier: CELLID)
        //设置cell高度
        tableView.rowHeight = 200
    }
    
}



//请求微博首页数据
    extension HMHomeTableViewController{
        
        fileprivate func loadData() {
//            //请求URL
//            let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
//            //请求参数
//            let params = ["access_token": HMUserAccountViewModel.shared.accessToken]
//            //发送请求
//            HMNetworkTools.shared.request(method: HTTPMethod.GET, urlString: urlString, paramters: params) { (reponse, error) in
//            HMNetworkTools.shared.homeLoadData{ (response, error) in
            homeViewModel.getHomeData { (isSuccess) in
                //请求失败
                if !isSuccess{
                    print("请求失败")
                    return
                }
//                print(reponse!)
                //判断是否为nil 而且是否为一个字典
                //在if-let或者guard-let中转类型的时候 一般情况 都需要使用的是as
//                guard let res = response as? [String: Any] else {
//                    return
//                }
                //判断是否为nil 而且是否为一个字典数组
//                guard let resArr = res["statuses"] as? [[String: Any]] else {
//                    return
//                }
//                //字典模型
//                let statusesArray = NSArray.yy_modelArray(with: HMHomeModel.self, json: resArr) as! [HMHomeModel]
//                print(statusesArray)
//                //赋值
//                self.dataArray = statusesArray
//                //刷新UI
//                self.tableView.reloadData()
                self.tableView.reloadData()
            }
            
        }
    }

//tableView带来的方法
extension HMHomeTableViewController{
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return homeViewModel.dataArray.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: CELLID, for: indexPath) as! HMHomeCell
        
        return cell
    }

}

extension HMHomeTableViewController{
        
    fileprivate func setNavBar() {

        navigationItem.rightBarButtonItem = UIBarButtonItem(imageName:"navigationbar_pop", target:self, action: #selector(btnClick))
    }
    
        @objc fileprivate func btnClick() {
            let demo = HMDemoViewController()
        
//                navigationItem.backBarButtonItem = UIBarButtonItem(title: "哈哈", style: .plain, target: self, action: #selector(back))
        
            navigationController?.pushViewController(demo, animated: true)
        }
    
        func back() {
            
            
        }

}
    

    

